package com.da.javatest.thread.juc.locksupport;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author chenlida
 * @date 2020/7/11 19:53
 *
 * 1.使用Condtion中的线程等待和唤醒方法之前，需要先获取锁。否者会报 IllegalMonitorStateException异常
 *
 * 2.signal()方法先于await()方法之前调用，线程无法被唤醒
 */
public class LockSupport2 {
    private static Lock lock = new ReentrantLock();
    private static Condition condition = lock.newCondition();

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            try {
                lock.lock();
                System.out.println(Thread.currentThread() + " start!");
                condition.await();
                System.out.println(Thread.currentThread() + " end!");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }).start();

        TimeUnit.SECONDS.sleep(3);
        //try {
        //    myLock.myLock();
        //    condition.signal();
        //} finally {
        lock.unlock();
        //}
    }
}
